<template>
  <div class="app-container">
    <el-row>
      <el-button type="primary" plain icon="Plus" @click="addShelf()">添加上样品类</el-button>
    </el-row>
    <el-table :data="tableData" highlight-current-row style="width: 50%; margin-top: 20px">
      <el-table-column property="categoryName" label="三级分类名称"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template #default="scope">
          <el-button link type="primary" @click="delectRow(scope.row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" title="" :before-close="handleClose" align-center>
      <shopClass v-if="dialogVisible" @submit="getSelection" @close="handleClose"></shopClass>
    </el-dialog>
  </div>
</template>

<script setup>
import { getCategorylist, addCategoryDict, deleteByCategroryId } from "@/api/system/rebateRules/protocol";
import { ElMessage, ElMessageBox } from "element-plus";

import shopClass from "./components/shopClass";

const dialogVisible = ref(false);
const tableData = ref([]);
const addShelf = () => {
  dialogVisible.value = true;
};
const getSelection = async e => {
  const categoryDictList = e.map(item => {
    return {
      categoryId: item.categoryId,
      categoryName: item.name
    };
  });
  const obj = {
    categoryDictList
  };
  const { code } = await addCategoryDict(obj);
  if (code == 200) {
    ElMessage({
      message: "添加成功",
      type: "success"
    });
    getArrylist();
    dialogVisible.value = false;
  }
};

const handleClose = () => {
  dialogVisible.value = false;
};
const getArrylist = async () => {
  const { data, code } = await getCategorylist();
  code == 200 && (tableData.value = data);
};
const delectRow = row => {
  ElMessageBox.confirm(`请确认是否删除${row.categoryName}协议？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    let obj = {
      categoryId: row.categoryId
    };
    const { code } = await deleteByCategroryId(obj);
    code == 200 &&
      ElMessage({
        message: "删除成功",
        type: "success"
      });
    getArrylist();
  });
};

getArrylist();
</script>

<style scoped lang="scss"></style>
